home *** CD-ROM | disk | FTP | other *** search
- Public Class SendKeysForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents Label1 As System.Windows.Forms.Label
- Friend WithEvents txtApplication As System.Windows.Forms.TextBox
- Friend WithEvents txtKeys As System.Windows.Forms.TextBox
- Friend WithEvents Label2 As System.Windows.Forms.Label
- Friend WithEvents btnSend As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.Label1 = New System.Windows.Forms.Label()
- Me.txtApplication = New System.Windows.Forms.TextBox()
- Me.txtKeys = New System.Windows.Forms.TextBox()
- Me.Label2 = New System.Windows.Forms.Label()
- Me.btnSend = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'Label1
- '
- Me.Label1.Location = New System.Drawing.Point(16, 16)
- Me.Label1.Name = "Label1"
- Me.Label1.Size = New System.Drawing.Size(304, 24)
- Me.Label1.TabIndex = 0
- Me.Label1.Text = "Application Title "
- '
- 'txtApplication
- '
- Me.txtApplication.Location = New System.Drawing.Point(16, 40)
- Me.txtApplication.Name = "txtApplication"
- Me.txtApplication.Size = New System.Drawing.Size(448, 24)
- Me.txtApplication.TabIndex = 1
- Me.txtApplication.Text = "Untitled - Notepad"
- '
- 'txtKeys
- '
- Me.txtKeys.Location = New System.Drawing.Point(16, 104)
- Me.txtKeys.Name = "txtKeys"
- Me.txtKeys.Size = New System.Drawing.Size(448, 24)
- Me.txtKeys.TabIndex = 1
- Me.txtKeys.Text = ""
- '
- 'Label2
- '
- Me.Label2.Location = New System.Drawing.Point(16, 80)
- Me.Label2.Name = "Label2"
- Me.Label2.Size = New System.Drawing.Size(304, 24)
- Me.Label2.TabIndex = 0
- Me.Label2.Text = "Keys"
- '
- 'btnSend
- '
- Me.btnSend.Location = New System.Drawing.Point(168, 144)
- Me.btnSend.Name = "btnSend"
- Me.btnSend.Size = New System.Drawing.Size(96, 40)
- Me.btnSend.TabIndex = 2
- Me.btnSend.Text = "Send"
- '
- 'SendKeysForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(488, 205)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.txtKeys, Me.Label2, Me.txtApplication, Me.Label1})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "SendKeysForm"
- Me.Text = "SendKeysForm"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' API declares
- Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
- Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Integer) As Integer
-
- ' send keys to selected app
-
- Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
- SendKeysToApplication(txtApplication.Text, txtKeys.Text)
- End Sub
-
- ' a reusable routine that sends specified keys to a given app
-
- Sub SendKeysToApplication(ByVal appTitle As String, ByVal keys As String)
- ' Find the other application.
- Dim hWnd As Integer = FindWindow(Nothing, appTitle)
- ' Exit if not found.
- If hWnd <= 0 Then
- MessageBox.Show("Application not found", "Error", _
- MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End If
- ' Make it the active application.
- SetForegroundWindow(hWnd)
- ' Send the keys and wait.
- SendKeys.SendWait(keys)
- End Sub
-
- End Class
-